package com.eobjectsoft.etranslator.core;

import com.eobjectsoft.etranslator.ETranslatorResources;
import com.eobjectsoft.etranslator.engine.*;
import com.eobjectsoft.etranslator.utils.LOG;
import java.io.*;
import org.eclipse.core.runtime.IProgressMonitor;

public class TranslateCore
{
    protected static long SLEEP_INTERVAL = 200L;

    public static void translateResourceFile(String path, TranslateMode mode, IProgressMonitor monitor)
        throws Exception
    {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try
        {
            int numOfKeys = getNumOfKeys(path);
            monitor.beginTask("", numOfKeys);
            String inputFileName = path;
            String outputFileName = TranslateUtils.getLocaleFileExtension(inputFileName, mode);
            LOG.debug("translate resource file:" + inputFileName);
            LOG.debug("to :" + outputFileName);
            TranslateEngine engine = TranslateEngine.getInstance();
            engine.setTranslateMode(mode);
            LOG.debug("translate mode set to:" + mode);
            reader = new BufferedReader(new FileReader(inputFileName));
            writer = new BufferedWriter(new FileWriter(outputFileName));
            String line = null;
            int keyIndex = 1;
            while((line = reader.readLine()) != null) 
                if(line.indexOf("=") > 0)
                {
                    String key = line.substring(0, line.indexOf("="));
                    String value = line.substring(line.indexOf("=") + 1);
                    monitor.subTask(ETranslatorResources.getString("subprogress.title") + " " + key);
                    String transValue = engine.translate(value);
                    if(transValue == null)
                        transValue = value;
                    Thread.sleep(SLEEP_INTERVAL);
                    LOG.debug("key=" + key + " value=" + value + " trans=" + transValue);
                    writer.write(key);
                    writer.write("=");
                    writer.write(transValue);
                    writer.newLine();
                    monitor.worked(1);
                    keyIndex++;
                }
        }
        catch(Exception e)
        {
            LOG.error(e);
            throw e;
        }
        finally
        {
            if(reader != null)
                try
                {
                    reader.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            if(writer != null)
                try
                {
                    writer.close();
                }
                catch(Exception we)
                {
                    we.printStackTrace();
                }
            monitor.done();
        }
        return;
    }

    private static int getNumOfKeys(String inputFileName)
    {
        int keysCount;
        BufferedReader reader;
        keysCount = 0;
        reader = null;
        int i;
        try {
			reader = new BufferedReader(new FileReader(inputFileName));
			for (String line = null; (line = reader.readLine()) != null;)
				if (line.indexOf("=") > 0)
					keysCount++;

			i = keysCount;
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
		}
    }


}
